package scheduler;

import java.util.*;
import util.DataBase;
import meal.Meal;

public class CustomScheduler implements Scheduler {
	private LinkedList<Meal> meals;
	private Comparator<Meal> comp;
	private double power=2;
	public CustomScheduler() {
		comp = new Comparator<Meal>() {
			public int compare(Meal m1, Meal m2) {
				return (int)(m1.getLastHad()-m2.getLastHad());
			}
		};
		this.meals = new LinkedList<Meal>();
		repopulate();
	}
	public Meal getNextMeal() {
		ListIterator<Meal> iter = meals.listIterator(0);
		Meal meal = null;
		while(iter.hasNext()) {
			meal = iter.next();
			if(Math.random()>Math.pow(meal.getRating(),power)) break;
		}
		if(meal!=null) {
			iter.remove();
			meals.offer(meal);
		}
		return meal;
	}
	public Meal getNextMeal(Attribute[] attr) {
		return getNextMeal();
	}

	private void repopulate() {
		this.meals=new LinkedList<Meal>();
		Meal[] tmp = util.IO.importMeals("recipes/");
		for(Meal meal : tmp)
			if(meal!=null) {
				this.meals.push(meal);
			}
		/*
		try {
			String names[] = DataBase.getMealName();
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.meals.clear();

		for(int i=0; i<names.length; i++) {
			this.meals.push(DataBase.getMeal(names[i]));
		} */
		Collections.sort(this.meals,comp);
	}
}
